from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
from qiskit.visualization import plot_bloch_multivector, plot_histogram
from qiskit_aer import AerSimulator
import matplotlib.pyplot as plt

# 初始化量子电路
qc = QuantumCircuit(1, 1)

# 初始状态可视化
print("初始状态：")
plot_bloch_multivector(Statevector.from_instruction(qc))
plt.title("Initial |0⟩ State")
plt.show()

# 应用哈达玛门
qc.h(0)

# 叠加态可视化
print("\n应用H门后的叠加态：")
plot_bloch_multivector(Statevector.from_instruction(qc))
plt.title("Superposition State after H-gate")
plt.show()

# 电路结构展示
print("\n量子电路结构：")
print(qc.draw(output='text'))

# 添加测量并运行实验
qc.measure(0, 0)
simulator = AerSimulator()
result = simulator.run(qc, shots=1000).result()
counts = result.get_counts()

# 测量结果可视化
print("\n测量统计：", counts)
plot_histogram(counts)
plt.title("Measurement Statistics")
plt.show()
